package com.clw.converter;


import com.clw.dataobject.DataDictionaryDetail;
import com.clw.dto.DictionaryDTO;
import com.clw.form.DataDictionaryForm;
import com.clw.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017-11-03.
 */
@Slf4j
public class DictionaryForm2DictionaryDTOConverter {

    public static DictionaryDTO convert(DataDictionaryForm form){
        Gson gson=new Gson();
        DictionaryDTO dto=new DictionaryDTO();
        if (StringUtils.isNotEmpty(form.getId())){
            dto.setId(form.getId());
        }
        dto.setName(form.getName());
        dto.setRemark(form.getRemark());
        dto.setCode(form.getCode());
        dto.setStatus(1);

        List<DataDictionaryDetail> list=new ArrayList<>();
        try {
            list=gson.fromJson(form.getDetail(),
                    new TypeToken<List<DataDictionaryDetail>>(){}.getType());
        }catch (Exception e){
            log.error("【对象转换】错误, string={}", form.getDetail());
        }
        dto.setList(list);
        return dto;
    }
}
